const express = require('express')
const router = express.Router()
const middleware = require('../middleware/index')
const errorCode = require('../error')

/**
 * 全局响应器 重写res.json
 * @param {*} req
 * @param {*} res
 * @param {*} next
 */
router.use((req, res, next) => {
  res.jsonSuccess = (data) => {
    res.json({
      code: 200,
      data,
      message: 'success'
    })
  }

  res.jsonFail = (code, message) => {
    res.json({
      code,
      msg: message || errorCode[code]
    })
  }

  next()
})

// 全局拦截
router.all('/*', middleware)

router.use(require('./module/books'))
router.use(require('./module/booksList'))
router.use(require('./module/user'))

module.exports = router
